﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WMS.BLL;
using WMS.Entity;

namespace WMS.App
{
    public partial class frmCategoryOperate : Form
    {
        private int _CategoryID = -1;
        private int _ParentID = -1;
        private Category _Cate = new Category();
        private CategoryAction _Action = CategoryAction.Add;
        private TreeNode _CurrentNode = null;
        public frmCategoryOperate()
        {
            InitializeComponent();
        }

        public void SetFormText(string text, int cateID, int parentID, string nodeName, CategoryAction action, TreeNode currentNode)
        {
            string caption = "";
            this.txtCategoryName.Text = "";
            _CategoryID = cateID;
            _ParentID = parentID;
            _Action = action;
            _CurrentNode = currentNode;
            switch (action)
            {
                case CategoryAction.Add:
                    caption = "增加【" + nodeName + "】的同级类别";
                    break;
                case CategoryAction.AddSubCategory:
                    caption = "增加【" + nodeName + "】的子类别";
                    break;
                case CategoryAction.Modify:
                    caption = "修改类别【" + nodeName + "】";
                    this.txtCategoryName.Text = nodeName;
                    break;
            }

            this.Text = caption;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            CategoryEntity entity = new CategoryEntity();
            entity.CategoryId = _CategoryID;
            entity.ParentId = _ParentID;
            entity.Name = this.txtCategoryName.Text.Trim();
            int lastNodeID = 0;
            if (string.IsNullOrEmpty(entity.Name))
            {
                return;
            }
            switch (_Action)
            {
                case CategoryAction.Add:
                    break;
                case CategoryAction.AddSubCategory:
                    entity.ParentId = _CategoryID;
                    break;
                case CategoryAction.Modify:
                    _Cate.Update(entity);
                    break;
            }
            if (_Action != CategoryAction.Modify)
            {
                TreeNode node = new TreeNode();
                _Cate.Add(entity);
                lastNodeID = _Cate.GetMaxID();
                node.Name = lastNodeID.ToString();
                node.Text = entity.Name;
                if (_Action == CategoryAction.Add && _CurrentNode.Name != "RootNode")
                {
                    _CurrentNode.Parent.Nodes.Add(node);                                        
                }
                else
                {
                    _CurrentNode.Nodes.Add(node);
                }
            }

            this.Hide();
        }
    }
}
